package com.scwatch.core.web.back;

import com.scwatch.common.orm.RowSide;
import com.scwatch.common.web.Servlets;
import com.scwatch.core.constant.Constants;
import com.scwatch.core.domain.HotWord;
import com.scwatch.core.domain.Site;
import com.scwatch.core.domain.User;
import com.scwatch.core.service.HotWordService;
import com.scwatch.core.service.OperationLogService;
import com.scwatch.core.support.Context;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.Map;

import static com.scwatch.core.constant.Constants.*;


@Controller
@RequestMapping("/core/hot_word")
public class HotWordController {
    private static final Logger logger = LoggerFactory
            .getLogger(HotWordController.class);

    @RequestMapping("list.do")
    @RequiresRoles("super")
    @RequiresPermissions("core:hot_word:list")
    public String list(
            @PageableDefault(sort = "id", direction = Sort.Direction.DESC) Pageable pageable,
            HttpServletRequest request, org.springframework.ui.Model modelMap) {
        Map<String, String[]> params = Servlets.getParamValuesMap(request,
                Constants.SEARCH_PREFIX);
        Integer siteId = Context.getCurrentSiteId();
        params.put("EQ_site.id",new  String[]{String.valueOf(siteId)});
        Page<HotWord> pagedList = hotWordService.findAll(params, pageable);
        modelMap.addAttribute("pagedList", pagedList);
        return "core/hot_word/hot_word_list";
    }

    @RequestMapping("create.do")
    @RequiresRoles("super")
    @RequiresPermissions("core:hot_word:create")
    public String create(Integer id, org.springframework.ui.Model modelMap) {
        if (id != null) {
            HotWord bean = hotWordService.get(id);
            modelMap.addAttribute("bean", bean);
        }
        modelMap.addAttribute(OPRT, CREATE);
        return "core/hot_word/hot_word_form";
    }

    @RequestMapping("edit.do")
    @RequiresRoles("super")
    @RequiresPermissions("core:hot_word:edit")
    public String edit(
            Integer id,
            Integer position,
            @PageableDefault(sort = "id", direction = Sort.Direction.DESC) Pageable pageable,
            HttpServletRequest request, org.springframework.ui.Model modelMap) {
        HotWord bean = hotWordService.get(id);
        Map<String, String[]> params = Servlets.getParamValuesMap(request,
                Constants.SEARCH_PREFIX);
        RowSide<HotWord> side = hotWordService.findSide(params, bean, position,
                pageable.getSort());
        modelMap.addAttribute("bean", bean);
        modelMap.addAttribute("side", side);
        modelMap.addAttribute("position", position);
        modelMap.addAttribute(OPRT, EDIT);
        return "core/hot_word/hot_word_form";
    }

    @RequestMapping("save.do")
    @RequiresRoles("super")
    @RequiresPermissions("core:hot_word:save")
    public String save(HotWord bean, String redirect,
                       HttpServletRequest request, RedirectAttributes ra) {
        Integer siteId = Context.getCurrentSiteId();
        Site site= Context.getCurrentSite();
        User user = Context.getCurrentUser();
        bean.setTime(new Date());
        bean.setUser(user);
        bean.setSite(site);
        bean = hotWordService.save(bean, siteId, user);
        logService.operation("opr.hotWord.add", bean.getName(), null,
                bean.getId(), request);
        logger.info("save HotWord, name={}.", bean.getName());
        ra.addFlashAttribute(MESSAGE, SAVE_SUCCESS);
        if (Constants.REDIRECT_LIST.equals(redirect)) {
            return "redirect:list.do";
        } else if (Constants.REDIRECT_CREATE.equals(redirect)) {
            return "redirect:create.do";
        } else {
            ra.addAttribute("id", bean.getId());
            ra.addAttribute("position", 0);
            return "redirect:edit.do";

        }
    }

    @RequestMapping("update.do")
    @RequiresRoles("super")
    @RequiresPermissions("core:hot_word:update")
    public String update(@ModelAttribute("bean") HotWord bean,
                         Integer position, String redirect, HttpServletRequest request,
                         RedirectAttributes ra) {
        Site site= Context.getCurrentSite();
        bean.setSite(site);
        hotWordService.update(bean);
        logService.operation("opr.hotWord.edit", bean.getName(), null,
                bean.getId(), request);
        logger.info("update HotWord, name={}.", bean.getName());
        ra.addFlashAttribute(MESSAGE, SAVE_SUCCESS);
        if (Constants.REDIRECT_LIST.equals(redirect)) {
            return "redirect:list.do";
        } else {
            ra.addAttribute("id", bean.getId());
            ra.addAttribute("position", position);
            return "redirect:edit.do";
        }
    }

    @RequestMapping("delete.do")
    @RequiresRoles("super")
    @RequiresPermissions("core:hot_word:delete")
    public String delete(Integer[] ids, HttpServletRequest request,
                         RedirectAttributes ra) {
        HotWord[] beans = hotWordService.delete(ids);
        for (HotWord bean : beans) {
            logService.operation("opr.hotWord.delete", bean.getName(),
                    null, bean.getId(), request);
            logger.info("delete HotWord, name={}.", bean.getName());
        }
        ra.addFlashAttribute(MESSAGE, DELETE_SUCCESS);
        return "redirect:list.do";
    }

    @ModelAttribute("bean")
    public HotWord preloadBean(@RequestParam(required = false) Integer oid) {
        return oid != null ? hotWordService.get(oid) : null;
    }

    @Autowired
    private OperationLogService logService;
    @Autowired
    private HotWordService hotWordService;
}
